<?php
/* The class is intended to map any given KOHV fs location to the real location on the computer's filesystem */

class System_FS_PathTool {
									  
									  
//*************************************************************************************************
/**
 * 
 * @param string $fs_root absolute path to the KOHV 'file system' root directory. 
 * @param string $lwd local working directory (cwd within 'file system')
 */
		private $fs_root;
	
		public function __construct($fsRoot) 
		{	
			
			
			
			$this->fsRoot = realpath($fsRoot);
			
			if ( $this->fsRoot == false )
				exit("ERROR: Wrong root directory for KOHV FS provided!");
			
			//echo "\n\n__construct() : $this->fsRoot \n\n";
		}
		
//*************************************************************************************************
/**
 * 
 * Converts an internal kohv fs path to an actual path of the computer's file system
 * @param unknown_type $localPath
 */
		public function localToGlobal($localPath)
		{
			
			$realPath = realpath($this->fsRoot .'/'.$localPath);
			
			//echo "\nlocalToGlobal(), realPath=$realPath \n";
			
			if($realPath===false) return false;

			$pos = strpos($realPath, $this->fsRoot);
			
			//echo "\nfsRoot = $this->fsRoot\n"; 
			//echo "\nlocal path: $localPath\n";
			//echo "\nglobal path: $realPath\n";
			
			// check if $realPath points to a dir outside of the kohv fs, return false if so.
			if ($pos === false)
			{ 
			    //echo "\n\nTrying to reach the outer world, huh?\n\n";
			    return  false;
			}
			return $realPath;			
		}
		
		
//*************************************************************************************************
		public function globalToLocal($globalPath)
		{
			
			$globalPath = realpath($globalPath);
			if ( $globalPath === false ) return false;
			
			$os = Core_Registry::read(Enum_RegistryItems::COMPUTER_OS);			
			if ($os === Core_Enum_OSTypes::WINDOWS)
			{
				$localPath = str_ireplace($this->fsRoot, "", $globalPath);
				$localPath = str_ireplace('\\', "/", $localPath);
			}
			else 
			{
				$localPath = str_replace($this->fsRoot, "", $globalPath);
			}
			
			/*echo "fs_root=$this->fsRoot\nglobalPath=$globalPath";			
			$localPath = str_ireplace($this->fsRoot, "", $globalPath);
			$localPath = str_ireplace('\\', "/", $localPath);*/
			
			if ($localPath === $globalPath)
			{
				echo "\nBad global path provided: $globalPath\n";
				return false;
			}
			
			if ($localPath=='') $localPath = '/';
			return $localPath;
			
		}
//*************************************************************************************************
		/*
		//TODO: Delete this method 
		private function parsePath($path)
		{
			$path = trim($path);
			//TODO: This must actually parse the ../../.. things to prevent a user from looking outside of the FS root!
			
			$firstSlash = "";
			$lastSlash = "";
			//if ($path[0])
			
			$parts = explode('/',$path);
			
			print_r($parts);
			
			 // /../../../../..
			
			return  $path;
		}
		*/
//*************************************************************************************************
/**
 * Checks if a directory exists or not.
 * @param $path path to a directory
 * @return true if the directory exists, false otherwise.
 */
		public function dirExist($localPath)
		{	
			//echo "PathTool/dirExist(), localPath = $localPath";
			$realPath = $this->localToGlobal($localPath);			
			//echo "PathTool/dirExist(), realPath = $realPath";
			
			if ($realPath === false) return false;
			return is_dir($realPath);
		}
		
		
//*************************************************************************************************
		public function __toString()
		{
			$res = "\n[PathTool]\n";
			$res.= "fs_root: " . $this->fsRoot;
			return $res . "\n";
		}
		
//*************************************************************************************************
/**
 * 
 * Validates a provided kohv path. Replaces multiple slashes with single ones
 * @param $path local kohv FS path
 * @return Returns re-slashed path if path was valid, otherwise returns false
 */
		private function validatePath($path)
		{
			// replace multiple slashes with the single ones 
			$path = preg_replace('%/{2,}%', '/', $path); //echo "\n\npath: $path\n\n";			
			
			
			// valid kohv path pattern
			$pattern = '%^(\/?[^\\<>|"*?:\r\n\s]+\/?)+$%';
						
			$res = preg_match($pattern, $path); //echo "\n\nres=$res\n\n";			
			
			if ((bool)$res) return $path;			
			return false;
		}
}